﻿using Fdb.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Text;

namespace Fdb
{
    /// <summary>
    /// 数据库操作工厂类
    /// </summary>
    public class DBHandlerFactory
    {
        //禁止产生类的实例
        private DBHandlerFactory() { }

        /// <summary>
        /// 直接以连接字符串和数据库类型构造
        /// </summary>
        /// <param name="connStr"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static DBHandler<T> GetHandler2<T>(string connectionString, DatabaseType databaseType)
        {
            switch (databaseType)
            {
                case DatabaseType.Mysql:
                    return new Fdb.Mysql.Manager<T>(connectionString, databaseType);
                case DatabaseType.SQLite:
                    return new Fdb.Sqlite.Manager<T>(connectionString, databaseType);
                case DatabaseType.SqlServer:
                    return new Fdb.SqlServer.Manager<T>(connectionString, databaseType);
                case DatabaseType.ODBC:
                    return null;
                case DatabaseType.OLEDB:
                    return null;
                default:
                    return null;
            }
        }

        /// <summary>
        /// 直接以连接字符串和数据库类型构造
        /// </summary>
        /// <param name="connStr"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static DBHandler<T> GetHandler<T>(string connectionString, DatabaseType databaseType)
        {
            //string connectionString = "";
            ////直接读取配置文件的连接字符串
            //if (databaseType == 0) //说明connection是连接字符串配置节名称
            //{
            //    connectionString = ConfigurationManager.ConnectionStrings[conection].ConnectionString;
            //}
            //else
            //{
            //    //connectionString = conection;
            //    connectionString = ConfigurationManager.AppSettings[conection];
            //    //XmlHelper.GetNodeAttribute(System.Windows.Forms.Application.ExecutablePath + ".config", "configuration/appSettings/add", "key", conection, "value");
            //}

            switch (databaseType)
            {
                case DatabaseType.Mysql:
                    return new Fdb.Mysql.Manager<T>(connectionString, databaseType);
                case DatabaseType.SQLite:
                    return new Fdb.Sqlite.Manager<T>(connectionString, databaseType);
                case DatabaseType.SqlServer:
                    return new Fdb.SqlServer.Manager<T>(connectionString, databaseType);
                case DatabaseType.ODBC:
                    return null;
                case DatabaseType.OLEDB:
                    return null;
                default:
                    return null;
            }
        }

        /// <summary>
        /// 直接以连接字符串和数据库类型构造
        /// </summary>
        /// <param name="connStr"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static DBHandler<T> GetHandler<T>(string connectionString, DatabaseType databaseType, DbTransaction dbTransaction)
        {
            //直接读取配置文件的连接字符串
            if (databaseType == 0)
            {
                connectionString = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;
            }

            switch (databaseType)
            {
                case DatabaseType.Mysql:
                    return new Fdb.Mysql.Manager<T>(connectionString, databaseType);
                case DatabaseType.SQLite:
                    return new Fdb.Sqlite.Manager<T>(connectionString, databaseType);
                case DatabaseType.SqlServer:
                    return new Fdb.SqlServer.Manager<T>(connectionString, databaseType, dbTransaction);
                case DatabaseType.ODBC:
                    return null;
                case DatabaseType.OLEDB:
                    return null;
                default:
                    return null;
            }
        }
    }
}
